//index.js
//获取应用实例
import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";

const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        theme: {},
        tabbar: {},
        isshow: true,
        alternateList: [
            {
                picture: 'https://m.xinwangzn.com:8081/file2/event/2020/09/28/ff8080817491155b0174d373543212c6.jpg',
                id: ""
            },
            {
                picture: 'https://m.xinwangzn.com:8081/file2/event/2020/09/28/ff8080817491155b0174d374f06c12c9.jpg',
                id: ""
            }

        ],
        msgList: [
            {
                title: "暂无通知公告",
                id: 1
            }, {
                title: '　',
                id: 2
            }
        ],
        shopList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: app.globalData.appTitle,    //页面标题
        })
        app.editTabBar();
        this.setData({
            theme: app.globalData.theme,
            tabbar: app.globalData.tabBar
        })
        // this.getSwiper();
        // this.getNotice();
        // this.getShopList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getSwiper();
        this.getNotice();
        this.getShopList();
        wx.setNavigationBarTitle({
            title: app.globalData.appTitle,    //页面标题
        })
        log.e(app.globalData.appTitle)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 轮播跳转
     * @param event
     */
    onSwiperTap: function (event) {
        const id = event.target.dataset.postId
        const content_id = event.target.dataset.postContent_id
        wx.navigateTo({
            url: '../index/swiper/swiper?id=' + id + "&content_id=" + content_id
        })
    },
    /**
     * 通知单条跳转
     * @param event
     */
    onNotice: function (event) {
        const id = event.target.dataset.postId
        const content_id = event.target.dataset.postContent_id
        log.e(JSON.stringify(event))
        wx.navigateTo({
            url: '../index/notice/notice?id=' + id + "&content_id=" + content_id
        })
    },
    /**
     * 通知列表跳转
     * @param event
     */
    onClickNotice: function () {
        wx.navigateTo({
            url: '../index/noticelist/noticelist'
        })
    },
    /**
     * 轮播图请求
     * type(1:欢迎页 2：轮播图 3：通知公告)
     */
    getSwiper: function () {
        const than = this
        http.get("notice/open/list", {
            type: "2",
            applet_id: app.globalData.applet_id,
            unit_code: app.globalData.unitCode,
        }).then(res => {
            if (res.data.code == 200) {
                const Swiper = res.data.data
                if (Swiper.length > 0) {
                    than.setData({
                        alternateList: Swiper
                    })
                } else {
                    var alternateList = [
                        {
                            picture: 'https://m.xinwangzn.com:8081/file2/event/2020/09/28/ff8080817491155b0174d373543212c6.jpg',
                            id: ""
                        },
                        {
                            picture: 'https://m.xinwangzn.com:8081/file2/event/2020/09/28/ff8080817491155b0174d374f06c12c9.jpg',
                            id: ""
                        }

                    ]
                    than.setData({
                        alternateList: alternateList
                    })
                }
            } else {

            }
        })
    },
    /**
     * 通知公告请求
     * type(1:欢迎页 2：轮播图 3：通知公告)
     */
    getNotice: function () {
        const than = this
        http.get("notice/open/list", {
            type: "3",
            applet_id: app.globalData.applet_id,
            unit_code: app.globalData.unitCode,
        }).then(res => {
            if (res.data.code == 200) {
                const Swiper = res.data.data
                if (Swiper.length > 0) {
                    than.setData({
                        msgList: Swiper
                    })
                    if (than.data.msgList.length == 1) {
                        than.setData({
                            text: res.data.data[0].title,
                            isshow: false
                        })
                    }
                } else {
                    var msgList = [
                        {
                            title: "暂无通知公告",
                            id: 1
                        }, {
                            title: '　',
                            id: 2
                        }
                    ]
                    than.setData({
                        isshow: true,
                        msgList: msgList
                    })
                }
            }
        })
    },
    /**
     * 雪亮工程
     * @param {*} e
     */
    onMonitor: function (e) {
        app.isLogin("../index/monitor/monitor")
    },
    /**
     * 社区活动
     */
    onActivity: function () {
        app.isLogin("../index/activity/activity")
    },
    /**
     * 天府容易办
     */
    indextoAppRYB: function () {
        wx.navigateToMiniProgram({
            appId: 'wx4b18951ae5c405cf',
            success(res) {
                // 打开成功
            }
        })
    },
    /**
     * 天府市民云
     */
    indextoAppSMY: function () {
        wx.navigateToMiniProgram({
            appId: 'wx65813e92ccd10f2f',
            success(res) {
                // 打开成功
            }
        })
    },
    /**
     * 物业保修
     * @param {*} e
     */
    onProperty: function (e) {
        //判断是否登录
        if (app.globalData.userInfo != null) {
            if (app.globalData.userInfo.people_id != null) {
                http.get("room/default", {
                    people_id: app.globalData.userInfo.people_id,
                    applet_id: app.globalData.userInfo.applet_id,
                }).then((res4) => {
                    app.globalData.room_id = res4.data.data.roomId
                })
                // 判断是否已经入住
                if (app.globalData.room_id != null) {
                    wx.navigateTo({
                        url: '../index/property/property',
                    })
                } else {
                    Dialog.confirm({
                        title: '温馨提示',
                        message: '暂未入住该小区，请问是否需要前往该小区入住？',
                        confirmButtonText: "前往",
                        cancelButtonText: "暂不"
                    })
                        .then(() => {
                            // on confirm
                            wx.navigateTo({
                                url: '../mine/room_register/bound_room/bound_room'
                            })
                        })
                        .catch(() => {
                            // on cancel
                        });
                }
            } else {
                Dialog.confirm({
                    title: '温馨提示',
                    message: '暂未实名认证，请问是否前往进行实名认证？',
                    confirmButtonText: "前往",
                    cancelButtonText: "暂不"
                })
                    .then(() => {
                        // on confirm
                        wx.navigateTo({
                            url: '../mine/room_register/room_register'
                        })
                    })
                    .catch(() => {
                        // on cancel
                    });
            }
        } else {
            wx.navigateTo({
                url: '/pages/mine/login/login',
            })
        }
    },

    /**
     * 获取推荐商家
     */
    getShopList: function () {
        http.get("around/page", {
            area_code: app.globalData.areaCode,
            is_recommend: 1,
            page_number: 1,
            page_size: 1000
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                this.setData({
                    shopList: res.data.data.list
                })
            }
        }, err => {

        })
    },

    /**
     * 商家详情
     * @param e
     */
    onShopDetails: function (e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: "../service_more/service_detail/service_detail?id=" + id
        })
    },

    /**
     * 更多服务
     * @param e
     */
    onServiceMore: function (e) {
        wx.switchTab({
            url: "../service_more/service_more"
        })
    },

    /**
     * 社区投票
     * @param e
     */
    onVote: function (e) {
        app.isLogin("./vote/vote")
    },

    /**
     * 社区风采
     * @param e
     */
    onStyle: function (e) {
        wx.navigateTo({
            url: "./community_style/community_style"
        })
    },

    /**
     * 建言献策，咨询建议
     * @param e
     */
    onSuggest: function (e) {
        app.isLogin("./suggest/suggest")
    },

    /**
     * 暂未开放
     */
    onNotOpen: function () {
        Dialog.alert({
            title: "温馨提示",
            message: "暂未开放，敬请期待"
        }).then(() => {

        })
    },
    /**
     * 党务政务
     */
    onParty: function () {
        app.isLogin("../index/party/party")
    },

    /**
     * 法律服务
     */
    onLawService: function (e) {
        app.isLogin("./law_service/law_service")
    },
    /**
     * 调解室
     */
    onMediation:function(){
        app.isLogin("./mediationroom/mediationroom")
    },

    /**
     * 都市熊猫村
     */
    onPandaVillage: function (e) {
        app.isLogin("./panda_village/panda_village")
    },

})